<template>
  <el-dialog
    width="70%"
    title="稽核记录"
    :visible="dialogVisible"
    @close="close"
  >
    <div class="crud-group">
      <el-select
        v-model="paramsForm.flag"
        placeholder="请选择标志"
        clearable
      >
        <el-option
          v-for="item in flagOption"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-input
        v-model="paramsForm.libraryTableName"
        type="text"
        placeholder="请输入库表名"
        clearable
      />
      <el-select
        v-model="paramsForm.taskState"
        placeholder="状态"
        clearable
      >
        <el-option
          v-for="item in stateOption"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <div style="display: flex">
        <el-button
          type="success"
          icon="el-icon-search"
          @click="getData"
        >
          搜索
        </el-button>
        <el-button
          type="warning"
          icon="el-icon-refresh-left"
          @click="resetForm"
        >
          重置
        </el-button>
      </div>
    </div>
    <el-table
      v-loading="isLoading"
      :data="data"
      border
    >
      <el-table-column
        prop="taskName"
        label="任务名"
        sortable
      />
      <el-table-column
        prop="flag"
        label="标志"
        sortable
      />
      <el-table-column
        prop="libraryTableName"
        label="库表名"
        sortable
      />
      <el-table-column
        prop="countNumber"
        label="数量"
        sortable
      />

      <el-table-column
        prop="taskState"
        label="状态"
        sortable
      >
        <template slot-scope="scope">
          <el-tag
            :type="getStateColor(scope.row.taskState)"
          >
            {{ scope.row.taskState }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column
        label="详情"
        min-width="60px"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="() => {
              tableDetail = scope.row.log || ''
              detailsDialogVisible = true;
            }
            "
          >
            查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
        :current-page="paramsForm.current"
        :page-size="paramsForm.size"
        layout="prev, pager, next,sizes,total"
        :total="total"
        @size-change="(val)=>{
          paramsForm.current = 1
          paramsForm.size = val
          getData()
        }"
        @current-change="(val)=>{
          paramsForm.current = val
          getData(false)
        }"
      />
    </div>

    <TableDetailsDialog
      :append-to-body="true"
      :dialog-visible="detailsDialogVisible"
      :details="tableDetail"
      @close="detailsDialogVisible = false"
    />
  </el-dialog>
</template>

<script>
import logAnalysisApi from '@/api/logAnalysis'
import TableDetailsDialog from './TableDetailsDialog.vue'

export default {
  components: {TableDetailsDialog},
  props: {
    dialogVisible: Boolean,
    taskId: String
  },
  data() {
    return {
      data: [],
      total: 0,
      isLoading: false,
      detailsDialogVisible: false,
      paramsForm: {
        current: 1,
        size: 10,
        libraryTableName: '',
        taskState: '',
        taskId: '',
        flag: ''
      },
      stateOption: [
        {
          label: '已完成',
          value: '已完成'
        },
        {
          label: '失败',
          value: '失败'
        },
        {
          label: '忽略',
          value: '忽略'
        },
        {
          label: '进行中',
          value: '进行中'
        }
      ],
      flagOption: [
        {
          label: '万国',
          value: '万国'
        },
        {
          label: '金桥',
          value: '金桥'
        }
      ],
      tableDetail: ''
    }
  },
  watch: {
    dialogVisible(val) {
      if (val) {
        this.paramsForm.taskId = this.taskId
        this.getData()
      }
    }
  },
  methods: {
    getData(resetPage = true) {
      if (resetPage) {
        this.paramsForm.current = 1
      }
      this.isLoading = true
      logAnalysisApi.getSyncFullAudit(this.paramsForm).then(res => {
        this.data = res.records
        this.total = res.total
      }).finally(() => {
        this.isLoading = false
      })
    },
    getStateColor(state) {
      if (state === '已完成' || state === '成功') {
        return 'success'
      } else if (state === '失败') {
        return 'danger'
      } else if (state === '进行中') {
        return 'warning'
      } else {
        return ''
      }
    },
    close() {
      this.$emit('close')
    },
    resetForm() {
      this.paramsForm = {
        size: this.paramsForm.size,
        current: 1,
        libraryTableName: '',
        taskState: '',
        taskId: this.taskId
      }
      this.getData()
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
